﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Taobao.EF;
using Taobao.Filter;
using Taobao.Manger;
using Taobao.Models;
using Taobao.Public;

namespace Taobao.Controllers
{
    [CheckLogin(true)]
    public class DetailController : BaseController
    {
        DbEntity db = new DbEntity();
        //
        // GET: /Detail/
        public ActionResult Index()
        {
            ViewBag.data = GetProInfo();
            return View();
        }
        public PartialViewResult Menu()
        {
            return PartialView();
        }
        /// <summary>
        /// 我的商品
        /// </summary>
        /// <returns></returns>
        public PartialViewResult MyProduct()
        {

            ViewBag.data = GetProInfo();
            return PartialView();
        }
        /// <summary>
        /// 充值页面
        /// </summary>
        /// <returns></returns>
        public PartialViewResult Charge()
        {
            var userInfo = db.UseInfors.Where(o => o.UserId == userId).FirstOrDefault();
            ViewBag.BuyCount = userInfo.BuyCount;
            ViewBag.SongCount = userInfo.SongCount;
            return PartialView();
        }
        /// <summary>
        /// 增加收藏的商品
        /// </summary>
        /// <param name="urls"></param>
        /// <param name="names"></param>
        /// <returns></returns>
        public JsonResult AddPro(string urls, string names)
        {
            if(string.IsNullOrEmpty(urls)||string.IsNullOrEmpty(names))
            {
               return CustomClass.GetJsonRet(NoticeMsg.必填数据不能为空);
            }
            var urllist = urls.Split('\n').ToList();
            var nameslist = urls.Split('\n').ToList();
            //校验 https://item.taobao.com/item.htm?id=xxxxxxxxx 
            return null;
        }
        /// <summary>
        /// 删除我收藏的商品
        /// </summary>
        /// <param name="urls"></param>
        /// <returns></returns>
        public JsonResult Delete(string urls)
        {

            JsonResult jr = new JsonResult();
            BaseModel bm = new BaseModel();
            if (string.IsNullOrEmpty(urls))
            {
                jr = CustomClass.GetJsonRet(NoticeMsg.没有数据);
                return jr;
            }
            var list = urls.Split(',').ToList();
            try
            {
                int? userId = Convert.ToInt32(ViewBag.userId);
                for (int index = 0; index < list.Count; index++)
                {
                    var url = list[index];
                    var item = db.ProductInfoes.Where(o => o.UserId == userId && o.url == url);
                    if (item != null)
                    {
                        var itmList = item.ToList();
                        foreach (ProductInfo itm in itmList)
                        {
                            db.ProductInfoes.Remove(itm);
                        }
                    }
                }
                db.SaveChanges();
                jr = CustomClass.GetJsonRet(NoticeMsg.成功);
                return jr;
            }
            catch (Exception ex)
            {
                jr = CustomClass.GetJsonRet(NoticeMsg.失败);
                return jr;
            }
        }
        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <returns></returns>
        public List<ProductInfo> GetProInfo()
        {
            var list = db.ProductInfoes.Where(o => o.UserId == userId).ToList();
            return list;
        }
        /// <summary>
        /// 充值
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public JsonResult UserCharge(int type)
        {
            return CustomClass.GetJsonRet(NoticeMsg.成功);
        }
        /// <summary>
        /// 消费
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public JsonResult Cost(int count, string proIds)
        {
            DBManager mangar = new DBManager();
            UseInfor userInfo = new UseInfor();
            //校验 数量  proId是否存在
            if (count == 0 || string.IsNullOrEmpty(proIds))
            {
                return CustomClass.GetJsonRet(NoticeMsg.消费流量参数不能为空);
            }
            var proList = proIds.Split(',').ToList();
            //var product = db.ProductInfoes.Where(o => o.url == proId).FirstOrDefault();
            //if (product == null)
            //{
            //    return CustomClass.GetJsonRet(NoticeMsg.该商品不存在);
            //}

            int scan = proList.Count * count;
            var canCost = mangar.CheckCount(userId, scan, ref  userInfo);
            if (userInfo == null)
            {
                return CustomClass.GetJsonRet(NoticeMsg.失败);
            }

            if (!canCost)
            {
                return CustomClass.GetJsonRet(NoticeMsg.余额不足);
            }

            var ret = mangar.Cost(userId, count, proList, userInfo);
            if (ret)
            {
                //执行刷新操作
                for (int k = 0; k < proList.Count; k++)
                {
                    Constant.UrlQueue.Enqueue(proList[k] + "," + count +","+ userId);
                }
                return CustomClass.GetJsonRet(NoticeMsg.成功);
            }
            return CustomClass.GetJsonRet(NoticeMsg.失败);
        }
    }
}